package com.imclient.swing;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import com.imclient.headler.LiveHeadler;
import com.imclient.headler.MessageHeadler;
import com.imclient.headler.ResultHeadler;
import com.imclient.message.Message;
import com.imclient.server.HeadlerServer;
import com.imclient.server.MessageClient;
import com.imclient.type.ConnInfoType;

public class Example extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4385637241400414374L;
	private JPanel contentPane;
	private JTextField serveraddrsss;
	private JTextField serverport;
	
	private MessageClient  messageClient;
	
	private JLabel status;
	private JTextArea sendMsg;
	private JTextArea recoiveMsg;
	
	private JLabel label_1;
	private JLabel tousername;
	private JTextField jieshoufang;
	private JTextField zhanghao;
	
	

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Example frame = new Example();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Example() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 582, 436);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel label = new JLabel("\u8FDE\u63A5\u5730\u5740");
		label.setBounds(10, 10, 54, 15);
		contentPane.add(label);
		
		serveraddrsss = new JTextField();
		serveraddrsss.setText("127.0.0.1");
		serveraddrsss.setBounds(64, 7, 118, 21);
		contentPane.add(serveraddrsss);
		serveraddrsss.setColumns(10);
		
		label_1 = new JLabel("\u63A5\u53E3");
		label_1.setBounds(192, 10, 32, 15);
		contentPane.add(label_1);
		
		serverport = new JTextField();
		serverport.setText("4646");
		serverport.setBounds(224, 7, 66, 21);
		contentPane.add(serverport);
		serverport.setColumns(10);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(26, 63, 364, 187);
		contentPane.add(scrollPane);
		
		recoiveMsg = new JTextArea();
		scrollPane.setViewportView(recoiveMsg);
		
		JScrollPane scrollPane_1 = new JScrollPane();
		scrollPane_1.setBounds(26, 260, 364, 94);
		contentPane.add(scrollPane_1);
		
		sendMsg = new JTextArea();
		scrollPane_1.setViewportView(sendMsg);
		
		JButton button = new JButton("\u53D1\u9001\u4FE1\u606F");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//sendMsg
				messageClient.sengMsgByUserId(jieshoufang.getText(), sendMsg.getText());
				
				
			}
		});
		button.setBounds(297, 364, 93, 23);
		contentPane.add(button);
		
		JButton button_1 = new JButton("\u5F00\u59CB\u8FDE\u63A5");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				messageClient = new MessageClient(){

					@Override
					public void connectInfo(ConnInfoType ct) {
						status.setText(ct.name());
					}
					
				};
				messageClient.setUserInfo(zhanghao.getText(),zhanghao.getText());
				messageClient.setNetInfo(serveraddrsss.getText(), Integer.parseInt(serverport.getText()));
				messageClient.setKeeplive(60000);
				messageClient.addHeadler(new MessageHeadler(){
					@Override
					public void receive(String from, String msg) {
						recoiveMsg.setText(recoiveMsg.getText()+from+":"+msg+"\r\n");
						
					}});
				
				messageClient.addHeadler(new LiveHeadler(){

					@Override
					public void receive(Message msg, HeadlerServer hs) {
						System.out.println("消停回馈");
						
					}});
				
				messageClient.addHeadler(new ResultHeadler(){

					@Override
					public void track(String messageid, String result) {
						
						System.out.println("messageid:"+messageid+"_____"+"result:"+result);
						
					}});
				
				new Thread(){
					
					public void run() {
						try {
							messageClient.start();
						} catch (Exception e1) {
							
							e1.printStackTrace();
							status.setText("网络中断");
						}
					};
					
				}.start();
				
			}
		});
		button_1.setBounds(346, 6, 93, 23);
		contentPane.add(button_1);
		
		tousername = new JLabel("接收方：");
		tousername.setBounds(400, 181, 54, 21);
		contentPane.add(tousername);
		
		status = new JLabel("\u72B6\u6001");
		status.setBounds(410, 214, 152, 21);
		contentPane.add(status);
		
		JButton button_2 = new JButton("\u5173\u95ED\u8FDE\u63A5");
		button_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(messageClient!=null){
					messageClient.close();
				}
				
			}
		});
		button_2.setBounds(451, 6, 93, 23);
		contentPane.add(button_2);
		
		jieshoufang = new JTextField();
		jieshoufang.setText("333333");
		jieshoufang.setBounds(451, 181, 106, 21);
		contentPane.add(jieshoufang);
		jieshoufang.setColumns(10);
		
		JLabel label_2 = new JLabel("帐号：");
		label_2.setBounds(400, 137, 54, 15);
		contentPane.add(label_2);
		
		zhanghao = new JTextField();
		zhanghao.setText("333333");
		zhanghao.setBounds(451, 134, 102, 21);
		contentPane.add(zhanghao);
		zhanghao.setColumns(10);
	}
}
